is_runing = false;
start_page = 2;

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var waterfall = {
		min:function(){
			var o= $("#waterfall").height(),s=1,m=$("#waterfall").find(".col");
			for(var i=0;i<4;i++){
				h=$(m.get(i)).height();
				
				if(h<=o){
					o=h;
					s=i+1;
				}
			}
			return s;
		},
		append:function(item){
			var id = "col"+this.min();
			$(item).appendTo($("#"+id));
		},
		start:function(url){
			if (in_ajax){
				start_page =1;
			}
			$(window).bind("scroll",function(){
				if( $(document).scrollTop() + $(window).height() > $(document).height() - 30 ){
					if(is_runing == true){
						return false;
					}
					if(start_page>7){
						return false;
					}
					is_runing = true;
					$("#page_loading").show();
					$.ajax({
					url:url+'&start_page='+start_page,
					dataType:'json',
					type:'get',
					success:function(d){
						for(var i=0;i<d.data.length;i++){
							waterfall.append(d.data[i]);
						}
						start_page+=1;
						if(start_page>7){
							$("#page").show();
						}else{
							$("#page").hide();
						}
						is_runing = false;
						$("#page_loading").hide();
					}
					});
			}
			});	
		}
}; /** end 瀑布流 */
$(".img").live("mouseover",function(){$(this).addClass("img-hover");});
$(".img").live("mouseout",function(){$(this).removeClass("img-hover");});

$.fn.extend({
        goTop:function () {
            if (this[0]) {
                var b =
                    this.click(function () {
                        $("html, body").animate({scrollTop:0}, 120)
                    }), c = null;
                $(window).bind("scroll", function () {
                    var d = $(document).scrollTop(), e = $(window).height();
                    0 < d ? b.fadeIn() : b.fadeOut();
                    $.cai.util.isIE6() && (b.hide(), clearTimeout(c), c = setTimeout(function () {
                        b.show();
                        clearTimeout(c)
                    }, 1E3), b.css("top", d + e - 125))
                })
            }
        }
	}
);

$(function() {
var $t_l = $('#goTop');
$(window).scroll(function(){
        var s_t = $(this).scrollTop();
        if(s_t > 100){
            $t_l.show();
        }else{
            $t_l.hide();
        }
    });

	$("#user").hover(function () {
		$(".dropdown",this).show();
  },function(){
		$(".dropdown",this).hide();
  });

	$(".favaImg").live("click",function(){
		var id = $(this).attr("id");
			$.ajax({
					url:"/item/like",
					dataType:'json',
					type:'POST',
					data:{id:id,_xsrf:$.cookie("_xsrf")},
					success:function(d){
						if(d.retCode == 200)
						{
							
						}
					},
						error:function(){
						alert("服务器错误");
					}
					});
		});
		$("#q").click(function(){
			if($(this).val() == "搜索商品"){
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val() == ""){
				$(this).val('搜索商品');
			}
		})
});

